// @Date         2021-11-17
// @Author       Aaron Zhang
// @Description
// @FileName      json.go

package formatter

import (
	"encoding/json"
	"errors"
	"fmt"
	"zin/framework/contract"
	"zin/framework/util"
	"time"
)

// JsonFormatter json 格式化
func JsonFormatter(level contract.LogLevel, t time.Time, msg string, fields map[string]interface{}) ([]byte, error) {
	mi := map[string]interface{}{
		"level":      getLevelPrefix(level),
		"time":       util.ConvertDate2String(t.Unix(), util.DateFormat1),
		"msg":        msg,
		"user_field": fields,
	}

	data, err := json.Marshal(mi)
	if err != nil {
		return nil, errors.New(fmt.Sprintf("jsonFormatter failed : %v", err))
	}

	return data, nil
}
